/**
 * @file    wCore_Table.h
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: wCore_Table.h 2460 2010-05-12 07:35:30Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this table of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this table of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_Widgets_Core_Table_H__
 #define __WOSH_Gui_Widgets_Core_Table_H__

 #include <framework/gui/IWidgetSerializable.h>
 #include <framework/gui/GuiUtilities.h>
 #include <QtGui/QTableView>
 #include <QAbstractTableModel>

 #include <core/Table.h>



namespace wosh {
 namespace gui {


// ----------------------------------------------------------------------------------------

class TableModel_Core_Table :  public QAbstractTableModel
 {
	Q_OBJECT

	public:
		TableModel_Core_Table(QObject *parent = 0);
		virtual ~TableModel_Core_Table();

	public:
		WRESULT loadObject( const wosh::Table* table );
		WRESULT acquireObject( wosh::Table* table );

	public:
		int rowCount(const QModelIndex &parent = QModelIndex()) const;
		int columnCount(const QModelIndex &parent = QModelIndex()) const;

		QVariant data(const QModelIndex &index, int role) const;
		QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;


	public: // sortable
		void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );

	public: // editable
		 Qt::ItemFlags flags(const QModelIndex &index) const;

		bool setData(const QModelIndex &index, const QVariant &value, int role);

	signals:
		void tableCell_changed( unsigned int row, unsigned int col, ISerializable* newValue );

	protected:
		wosh::Table* dataTable;

}; // class def

// ----------------------------------------------------------------------------------------


class wCore_Table :	public QTableView,
					public virtual IWidgetSerializableT<wosh::Table>
 {
	Q_OBJECT

	WOSH_WIDGET_SERIALIZABLE(wosh::gui::wCore_Table)

	public:
		wCore_Table(QWidget* parent = NULL);
		virtual ~wCore_Table();

	public:
		virtual WRESULT loadObject( const wosh::Table* object );
		virtual WRESULT saveObject( wosh::Table* object ) const;

	public:
		inline const TableModel_Core_Table* getTableModel() const	{ return this->tableModel; }
		inline TableModel_Core_Table* getTableModel()				{ return this->tableModel; }


	signals:
		void updatedObject();

	protected:
		TableModel_Core_Table* tableModel;


}; // class def



// ----------------------------------------------------------------------------------------




 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_Widgets_Core_Table_H__
